package com.tianyalei.wolf.sample.activity.wxcircle.adater.common;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

/**
 * Created by wolf on 2016/3/30.
 */
public abstract class WXCommonAdapter<T> extends BaseAdapter {
    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List<T> mDatas;
    protected final int mItemLayoutId;


    public WXCommonAdapter(Context context, List<T> mDatas, int itemLayoutId) {
        mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.mDatas = mDatas;
        this.mItemLayoutId = itemLayoutId;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        WXViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new WXViewHolder(mContext, parent, mItemLayoutId, position);
            initLayout(viewHolder, position);
        } else {
            viewHolder = (WXViewHolder) convertView.getTag();
        }

        convert(viewHolder, getItem(position));
        return viewHolder.getConvertView();
    }

    public abstract void convert(WXViewHolder helper, T item);

    public abstract void initLayout(WXViewHolder helper, int position);


    @Override
    public int getCount() {
        return mDatas.size();
    }

    @Override
    public T getItem(int position) {
        return mDatas.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

}
